package com.joey.protocol;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.internal.Util;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;
import okio.Sink;
import okio.Source;

/**
 * Created by Joey on 2018/4/4.
 */

public abstract class UploadRequestBody extends RequestBody {

    MediaType contentType = MultipartBody.FORM;
    File file;
    OnUploadListener listener;
    private int index;

    public UploadRequestBody(File file, int index, OnUploadListener listener) {
        this.index = index;
        this.file = file;
        this.listener = listener;
    }

    public UploadRequestBody(File file, int index) {
        this(file, index, null);
    }

    public void setListener(OnUploadListener listener) {
        this.listener = listener;
    }

    @Nullable
    @Override
    public MediaType contentType() {
        return contentType;
    }

    @Override
    public long contentLength() {
        return file.length();
    }

    @Override
    public void writeTo(BufferedSink sink) throws IOException {
        Source source;
        try {
            source = Okio.source(file);
            //sink.writeAll(source);
            Buffer buf = new Buffer();
            Long remaining = contentLength();
            for (long readCount; (readCount = source.read(buf, 2048)) != -1; ) {
                sink.write(buf, readCount);
                if (listener != null)
                    listener.onSink(contentLength(), remaining -= readCount, index, remaining == 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static RequestBody createCustomRequestBody(final MediaType contentType, final int index, final OnUploadListener listener, final File file) {
        return new RequestBody() {

            @Override
            public MediaType contentType() {
                return contentType;
            }

            @Override
            public long contentLength() {
                return file.length();
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                Source source;
                try {
                    source = Okio.source(file);
                    //sink.writeAll(source);
                    Buffer buf = new Buffer();
                    Long remaining = contentLength();
                    for (long readCount; (readCount = source.read(buf, 2048)) != -1; ) {
                        sink.write(buf, readCount);
                        if (listener != null)
                            listener.onSink(contentLength(), remaining -= readCount, index, remaining == 0);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }
}
